import { Component, OnDestroy, Input, OnInit, Output, EventEmitter } from "@angular/core";
import { ManhuaFavorProvider } from "./manhua-favor.provider";
import { Subscription } from 'rxjs/Subscription';

@Component({
    selector: 'manhua-favor',
    templateUrl: './manhua-favor.html',
    providers: [ManhuaFavorProvider]
})

export class ManhuaFavorComponent {

    @Input('manhua') manhua: any;
    @Input('chapter') _chapter:any;
    isFavor: boolean = false;

    readNowChapterHref:string = '';

    @Output('FavorEmit') FavorEmit: EventEmitter<any> = new EventEmitter;

    constructor(private service: ManhuaFavorProvider) { }

    ngOnInit() {


        if (this.manhua["id"]) {
            this.manhua["_id"] = null;
            this.manhua["_id"] = this.manhua["id"];
        }
        this.getIsFovarHttp(this.manhua);
    }

    // 更新
    updateAjaxSub:Subscription;
    updateFavor(_chapter){
        let json = {
            "_id": this.manhua['_id'],
            "readChapter": _chapter ? _chapter['chapter'] : '',
            "readChapterHref": _chapter ? _chapter['href'] : '',
        };
        this.updateAjaxSub = this.service.updateFavor(json).subscribe((res: any) => {
            console.log(res);
            if (res.success) {
                

            } 


        }, (err: any) => {

        });
    }

    // 收藏
    ajaxSub: Subscription;
    favor(_chapter) {

        if (this.ajaxSub) {
            this.ajaxSub.unsubscribe();
        }
        let json = {
            "_id": this.manhua['_id'],
            "title": this.manhua['title'],
            "readChapter": _chapter ? _chapter['chapter'] : '',
            "readChapterHref": _chapter ? _chapter['href'] : '',
            "img": this.manhua['img'],
            "chapterLength": this.manhua['chapters'].length || 0
        };
        this.ajaxSub = this.service.favor(json).subscribe((res: any) => {
            // console.log(res);
            if (res.success) {
                this.isFavor = true;

            } else {
                this.isFavor = false;
            }


        }, (err: any) => {

        });
    }

    // 取消收藏
    ajaxUnSub: Subscription;
    unfavor() {

        if (this.ajaxUnSub) {
            this.ajaxUnSub.unsubscribe();
        }
        let json = {
            "_id": this.manhua['_id']
        };
        this.ajaxUnSub = this.service.unfavor(json).subscribe((res: any) => {
            console.log(res);
            if (res.success) {
                this.isFavor = false;

            } else {
                this.isFavor = true;

            }
        }, (err: any) => {

        });
    }

    // 是否已收藏状态
    ajaxIsFavorSub: Subscription;
    getIsFovarHttp(param) {
        let json = { "manhuaId": param['_id'] };
        this.ajaxIsFavorSub = this.service.isFavor(json).subscribe((res: any) => {
            console.log(res);

            if (res.success && res.data.length > 0) {

                this.readNowChapterHref = res.data[0]['readChapterHref'];
                this.isFavor = true;

            } else {
                this.isFavor = false;

                this.readNowChapterHref = '';
            }

            // 向父组件广播是否收藏
            this.FavorEmit.emit(this.isFavor);
        });
    }


    ngOnDestroy() {
        if (this.ajaxSub) {
            this.ajaxSub.unsubscribe();
        }
        if (this.ajaxIsFavorSub) {
            this.ajaxIsFavorSub.unsubscribe();
        }
        if (this.ajaxUnSub) {
            this.ajaxUnSub.unsubscribe();
        }

        if(this.updateAjaxSub){
            this.updateAjaxSub.unsubscribe();
        }
    }

}